﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using DevMaker.SIC.Model;
using log4net;


namespace DevMaker.SIC.Dao {
    public class UsuarioDao {
        private static readonly ILog Log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

        public List<Usuario> ObtieneTodos() {
            const string storeName = "SP_Usuarios_L";
            var list = new List<Usuario>();
            var conn = new SqlConnection(ConfigurationManager.ConnectionStrings["SIC_Connection"].ToString());
            try {
                conn.Open();
                var command = new SqlCommand(storeName, conn) { CommandType = CommandType.StoredProcedure };
                var reader = command.ExecuteReader();
                while (reader.Read()) {
                    var u = new Usuario();
                    u.ReadFromDataReader(reader);
                    list.Add(u);
                }
            } catch (Exception ex) {
                Console.Write(ex.InnerException);
            } finally {
                conn.Close();
            }
            return list;
        }

        public List<Usuario> ObtieneCajerosSinTurnoActivo() {
            var list = new List<Usuario>();
            var conn = new SqlConnection(ConfigurationManager.ConnectionStrings["SIC_Connection"].ToString());
            try {
                conn.Open();
                var command = new SqlCommand("sp_usuarios_cajeros_sin_turno_activo_l", conn) { CommandType = CommandType.StoredProcedure };
                var reader = command.ExecuteReader();
                while (reader.Read()) {
                    var u = new Usuario();
                    u.ReadFromDataReader(reader);
                    list.Add(u);
                }
            } catch (Exception ex) {
                Log.Error(ex);
                throw;
            } finally {
                if (conn.State != ConnectionState.Closed)
                    conn.Close();
            }
            return list;
        }

        public List<Usuario> ObtieneCajerosConTurnoActivo() {
            var list = new List<Usuario>();
            var conn = new SqlConnection(ConfigurationManager.ConnectionStrings["SIC_Connection"].ToString());
            try {
                conn.Open();
                var command = new SqlCommand("sp_usuarios_cajeros_con_turno_activo_l", conn) { CommandType = CommandType.StoredProcedure };
                var reader = command.ExecuteReader();
                while (reader.Read()) {
                    var u = new Usuario();
                    u.ReadFromDataReader(reader);
                    list.Add(u);
                }
            } catch (Exception ex) {
                Log.Error(ex);
                throw;
            } finally {
                if (conn.State != ConnectionState.Closed)
                    conn.Close();
            }
            return list;
        }
    }
}